package com.grsu.phonestation.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.grsu.phonestation.dao.PaymentDAO;
import com.grsu.phonestation.dao.impl.base.AbstractHibernateDAO;
import com.grsu.phonestation.domain.Client;
import com.grsu.phonestation.domain.Payment;

@Repository
public class PaymentDAOImpl<P extends Payment> extends
		AbstractHibernateDAO<P, Integer> implements PaymentDAO<P> {

	@SuppressWarnings("unchecked")
	protected Class<P> getGenericEntityClass() {
		return (Class<P>) Payment.class;
	}

	@SuppressWarnings("unchecked")
	@Override
	public P findById(Integer paymentId) {
		Criteria criteria = getSession().createCriteria(
				this.getGenericEntityClass(), "payment").add(
				Restrictions.eq("id", paymentId));
		return (P) criteria.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<P> getPaymentClient(Client client) {
		Criteria criteria = getSession().createCriteria(
				this.getGenericEntityClass(), "payment").add(
				Restrictions.eq("user", client));
		List<P> payments = criteria.list();
		return payments;
	}

}
